package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityAsrCheckFormDetail;
import cc.comac.mom.dao.entity.QualityAsrCheckFormDetailDao;
import cc.comac.mom.dao.entity.QualityAsrCheckFormDetailDeleteHistoryDao;
import cc.comac.mom.entity.QualityAsrCheckFormDetailDeleteHistoryEntity;
import cc.comac.mom.entity.QualityAsrCheckFormDetailEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.TimeUtil;
@Component
@Transactional
public class QualityAsrCheckFormDetailDataHandler extends ServiceImpl<QualityAsrCheckFormDetailDao, QualityAsrCheckFormDetailEntity>{

	@Autowired
	private QualityAsrCheckFormDetailDao qualityAsrCheckFormDetailDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Autowired
	private QualityAsrCheckFormDetailDeleteHistoryDao qualityAsrCheckFormDetailDeleteHistoryDao;
	// 通过检验表单主键查询对应的检验数据
	public List<QualityAsrCheckFormDetailEntity> selectByFormId(long formId) {
		QueryWrapper<QualityAsrCheckFormDetailEntity> queryWrapper = new QueryWrapper<QualityAsrCheckFormDetailEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("asr_form_id", formId);
		List<QualityAsrCheckFormDetailEntity> selectList = qualityAsrCheckFormDetailDao.selectList(queryWrapper);
		return selectList;
	}
	
	
	// 通过材料号或零件号查询对应的检验数据
		public List<QualityAsrCheckFormDetailEntity> selectByPartNumber(String partNumber) {
			QueryWrapper<QualityAsrCheckFormDetailEntity> queryWrapper = new QueryWrapper<QualityAsrCheckFormDetailEntity>();
			queryWrapper.eq("row_status", 1);
			queryWrapper.eq("part_number", partNumber);
			List<QualityAsrCheckFormDetailEntity> selectList = qualityAsrCheckFormDetailDao.selectList(queryWrapper);
			return selectList;
		}
	
	 
//	// 删除表单详情
//	public ResultBean deleteAsrCheckFormDetail (QualityAsrCheckFormDetail qualityAsrCheckFormDetail) {
//		ResultBean resultBean = new ResultBean(true);
//		QualityAsrCheckFormDetailEntity innerData = qualityAsrCheckFormDetail.getInnerData();
//		if (innerData.getId() == 0) {
//			throw new RuntimeException("无法获取当前数据");
//		}
//		// 预留删除校验
//		// 。。。。。。。
//		
//		QualityAsrCheckFormDetailEntity qualityAsrCheckFormDetailEntity = new QualityAsrCheckFormDetailEntity();
//		qualityAsrCheckFormDetailEntity.setId(innerData.getId());
//		qualityAsrCheckFormDetailEntity.setRowStatus(0);
//		baseEntityDataUtil.updateBaseEntityData(qualityAsrCheckFormDetailEntity);
//		qualityAsrCheckFormDetailDao.updateById(qualityAsrCheckFormDetailEntity);
//		return resultBean;
//	}
	
	// 新增删除修改表单详情数据
	public ResultBean batchInsertAsrCheckFormDetail(List<QualityAsrCheckFormDetail> list) {
		ResultBean resultBean = new ResultBean(true);
		List<QualityAsrCheckFormDetailEntity> formDetailList = new ArrayList<QualityAsrCheckFormDetailEntity>();
		for (QualityAsrCheckFormDetail qualityAsrCheckFormDetail : list) {
			QualityAsrCheckFormDetailEntity innerData = qualityAsrCheckFormDetail.getInnerData();
			
			// 当是新增时
			if (innerData.getId() == 0) {
				baseEntityDataUtil.setBaseEntityData(innerData);
			} else {
				// 当数据为删除时更新历史信息
				if (innerData.getRowStatus() == 0) {
					QualityAsrCheckFormDetailDeleteHistoryEntity qualityAsrCheckFormDetailDeleteHistoryEntity = new QualityAsrCheckFormDetailDeleteHistoryEntity();
					long id = baseEntityDataUtil.getId();
					qualityAsrCheckFormDetailDeleteHistoryEntity.setId(id);
					qualityAsrCheckFormDetailDeleteHistoryEntity.setAsrFormDetailId(innerData.getId());
					qualityAsrCheckFormDetailDeleteHistoryEntity.setAsrFormId(innerData.getAsrFormId());
					qualityAsrCheckFormDetailDeleteHistoryEntity.setFeatures(innerData.getFeatures());
					qualityAsrCheckFormDetailDeleteHistoryEntity.setFeaturesCount(innerData.getFeaturesCount());
					qualityAsrCheckFormDetailDeleteHistoryEntity.setGrade(innerData.getGrade());
					qualityAsrCheckFormDetailDeleteHistoryEntity.setInitialReliabilityValue(innerData.getInitialReliabilityValue());
					qualityAsrCheckFormDetailDeleteHistoryEntity.setSize(innerData.getSize());
					qualityAsrCheckFormDetailDeleteHistoryEntity.setCreationTime(TimeUtil.getLocalDateTime());
					qualityAsrCheckFormDetailDeleteHistoryEntity.setCreator(baseEntityDataUtil.getCurrentUser());
					qualityAsrCheckFormDetailDeleteHistoryDao.insert(qualityAsrCheckFormDetailDeleteHistoryEntity);
				}
				// 当是修改时
				baseEntityDataUtil.updateBaseEntityData(innerData);
			}
			formDetailList.add(innerData);
		}
		saveOrUpdateBatch(formDetailList, formDetailList.size());
		return resultBean;
	}
	
	
	
}
